// pages/detail/detail.js
let request = require('../../utils/_request');
const app = getApp()
// 全局变量
const {
  baseUrl,
  audioManager //播音组件
} = app.globalData

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: getApp().globalData.userInfo,
    phoneNum: getApp().globalData.phoneNum,
    id: 0,
    type: '',
    dealwith: '',
    eventListObj: {
      // "todoId": 1,
      // "eventName": "测试事件名称",
      // "eventType": "基层事务管理",
      // "happenTime": "\r\n2022-04-28 16:41",
      // "happenAddress": "新北社区",
      // "eventNature": null,
      // "eventDesc": "我住新乐中街2号8栋3单元1号，家里有个3个多月的婴儿，还有个60多岁的老父亲，由于家里一楼，挨着新乐北巷2号的车棚，此处长期有人下午大声打牌，严重影响了我孩子的休息生长以及我父亲下午的午休，我已经反映了很多次，但是没有任何效果，反而他们变本加厉，这次儿保医生说我小孩身高及体重增长率不够，怀疑是未休息好所导致。请问街道办的同志能不能想点好的办法？每次直接去说他们根本就不听，没人能治得了他们了吗？我家小孩和老人就要平白无故忍受这一切吗？",
      // "handleTime": null,
      // "handleInfo": null
    },
    // 控件
    options: {
      currentClickItem: '',
      recordStatus: [0, 0, 0] //-1暂停,1播放中,0一般
    },
    // 表单数据
    formData: {
      eventName: '',
      eventType: [],
      text: '',
      address: '',
      level: '',
      time: '',
    },
    levelItems: ['一般', '急', '特急'],
    photosList: [],
    videosList: [],
    recordList: [],
    mediaList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type,
      id: options.id,
      dealwith: options.dealwith,
    })
    this.detailInfo(options.id)
    this.initRecord()
    let that = this
    getApp().watchUserInfo(that.watchBackUserInfo)
    getApp().watchPhoneNum(that.watchBackPhoneNum)
  },
  watchBackUserInfo(userInfo) {
    console.log('监听到的数据userInfo：' + userInfo)
    let that = this
    that.setData({
      userInfo: userInfo
    })
  },
  watchBackPhoneNum(phoneNum) {
    console.log('监听到的数据phoneNum：' + phoneNum)
    let that = this
    that.setData({
      phoneNum: phoneNum
    })
  },
  // 详情信息
  detailInfo(id) {
    let that = this
    let url = baseUrl + '/api/eventinfo/info?id=' + id

    wx.showLoading({
      title: '加载中...',
    })
    request.$get({
      url: '/api/eventinfo/info?id=' + id,
      onSuccess: function (res) {
        if (res.data.code === 0) {
          console.log(res.data)


          let {
            picUrlList,
            videoUrlList,
            voiceUrlList
          } = res.data.eventInfo

          picUrlList = picUrlList ? picUrlList.map(item => {
            return {
              url: 'http://171.217.24.24:8017' + item,
              isImage: true,
              type: 'image',
              thumb: 'http://171.217.24.24:8017' + item
            }
          }) : []
          videoUrlList = videoUrlList ? videoUrlList.map(item => {
            return {
              url: 'http://171.217.24.24:8017' + item,
              isVideo: true
            }
          }) : []
          voiceUrlList = voiceUrlList ? voiceUrlList.map(item => {
            return 'http://171.217.24.24:8017' + item
          }) : []

          let mediaList = picUrlList.concat(videoUrlList)

          that.setData({
            eventListObj: res.data.eventInfo,
            photosList: picUrlList,
            videosList: videoUrlList,
            recordList: voiceUrlList,
            mediaList
          })
        }
        console.log(res)
      },
    })
    // wx.request({
    //   url,
    //   header: {
    //     "authorization": "bearer" + wx.getStorageSync('token')
    //   },
    //   method: 'GET',
    //   fail: function (err) {
    //     console.log(err)
    //   },
    //   complete() {
    //     wx.hideLoading({
    //       success: (res) => {},
    //     })
    //   }
    // })
  },

  // 事件类别
  eventTypeChange(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    picker.setColumnValues(1, eventTypes[value[0]]);
  },

  // 文本双向绑定
  setFormValue(e) {
    let {
      formData,
      options
    } = this.data
    console.log(e.target)
    console.log(e.detail.value)
    formData[e.target.dataset.name] = e.detail.value
    options.showPopup = false
    this.setData({
      formData,
      options
    })
  },
  // 选择框双向绑定
  bindPickerChange(e) {
    let formData = this.data.formData
    let objectArray = this.data[e.target.dataset.optionsName]
    formData[e.target.dataset.name] = objectArray[e.detail.value].id
    formData[e.target.dataset.name + 'Label'] = objectArray[e.detail.value].name
    this.setData({
      formData
    })
  },


  //  级联选择器列变化
  mulPickerColChange(e) {
    if (e.detail.column === 0) {
      let objectArray2 = this.data.objectArray2
      objectArray2[e.detail.column + 1] = this.data.objectArrayTree[e.detail.value].children
      this.setData({
        objectArray2: objectArray2
      })
    }
  },
  //  级联选择器双向绑定数据
  mulPickerChange(e) {
    let formData = this.data.formData
    let obj = this.data[e.target.dataset.optionsName][e.detail.value.length - 1][e.detail.value[e.detail.value.length - 1]]
    formData[e.target.dataset.name] = obj.id
    formData[e.target.dataset.name + 'Label'] = obj.name
    this.setData({
      formData
    })
    console.log(obj, this.data.formData)
  },
  getMap() {
    let that = this
    wx.chooseLocation({
      success: function (res) {
        let name = res.name
        let address = res.address
        let latitude = res.latitude
        let longitude = res.longitude
        let formData = that.data.formData
        formData.address = res.address
        that.setData({
          formData
        })
        console.log(res)
      }
    })
  },
  /**
   * 初始化语音识别回调
   * 绑定语音播放开始事件
   */
  initRecord: function () {
    let {
      recordList,
      options
    } = this.data
    let that = this
    // 音频播放开始回调
    audioManager.onPlay(() => {
      // wx.showToast({
      //   title: '开始播放',
      //   duration: 500
      // })
    })
    // 音频播放结束回调
    audioManager.onEnded(() => {
      options.recordStatus = [0, 0, 0]
      this.setData({
        options
      })
    })
  },
  //音频播放
  playAudio(e) {
    let options = this.data.options
    let {
      path: src,
      index
    } = e.currentTarget.dataset

    if (audioManager.src === src && options.recordStatus[index] === 1) {
      // 暂停
      audioManager.pause()
      options.recordStatus = [0, 0, 0]
      options.recordStatus[index] = -1
    } else {
      // 播放
      audioManager.src = src
      audioManager.play()
      options.recordStatus = [0, 0, 0]
      options.recordStatus[index] = 1
    }

    this.setData({
      options
    })
  },
  previewImage(e) {
    var current = e.currentTarget.dataset.src;
    console.log(this.data.photosList)
    let urls = this.data.photosList.map(item => item.url)
    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: urls, // 需要预览的图片http链接列表
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log(res.target.dataset.share, '09876')
    let title = this.data.eventListObj.eventtitle
    let id = this.data.id
      if(res.target.dataset.share ==="metting"){
        return {
          title:`邀请您加入"${title}"相关会议`,
          path:'/pages/share/share',
          imageUrl:'https://cdn.meeting.tencent.com/assets/configurable-website/packages/meeting-public/meeting_logo.png'
        }
      } else if(res.target.dataset.share ==="notice") {
        return {
          title:`事件上报成功`,
          path:`/pages/detail/detail?id=${id}`
        }
      } else {
        return {
          title: '数字人慧勤务平台',
          desc: '数字人慧勤务平台',
          path: `/pages/detail/detail?id=${id}` // 路径，传递参数到指定页面。
         }
      }
  }
})